package cc.iotkit.screen.controller;

import cc.iotkit.common.api.Request;
import cc.iotkit.common.excel.utils.ExcelUtil;
import cc.iotkit.screen.HuToolExcelUtils;
import cc.iotkit.screen.dao.*;
import cc.iotkit.screen.dto.*;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = {"大屏数据"})
@Slf4j
@RestController
@RequestMapping("/screen")
public class StatisticsController {
    @Autowired
    private OnlineStatisticsRepository onlineStatisticsRepository;

    @Autowired
    private JobRankingRepository jobRankingRepository;
    @Autowired
    private JobStatisticsRepository jobStatisticsRepository;
    @Autowired
    private CleanStatisticsRepository cleanStatisticsRepository;
    @Autowired
    private FenceAlertRepository fenceAlertRepository;
    @Autowired
    private DeviceStatisticsRepository deviceStatisticsRepository;

    @ApiOperation("使用时长排名")
    @PostMapping("/online_times/{period}/{date}")
    public List<Map<String,Object>> getOnlineTimes(@PathVariable String period,@PathVariable String date) {
        List<Map<String,Object>> result= onlineStatisticsRepository.getStatistics(period,date);
        return result;
    }

    @ApiOperation("工作数据排名")
    @PostMapping("/job_ranking/{period}/{date}/{category}")
    public List<Map<String,Object>> getJobRanking(@PathVariable String period,@PathVariable String date,@PathVariable String category) {
        List<Map<String,Object>> result= jobRankingRepository.getStatistics(period,date,category);
        return result;
    }

    @ApiOperation("工作数据统计")
    @PostMapping("/job_statistics/{period}/{date}/{category}")
    public List<Map<String,Object>> getJobStatistics(@PathVariable String period,@PathVariable String date,@PathVariable String category) {
        List<Map<String,Object>> result= jobStatisticsRepository.getStatistics(period,date,category);
        return result;
    }

    public List<DeviceSquareVo> getSquareByDevice(String deviceId,String period,String beginDate,String endDate) {
        List<Map<String,Object>> result= deviceStatisticsRepository.getSquareByDevice(deviceId,period,beginDate,endDate);
        List<DeviceSquareVo> res=new ArrayList<>();
        result.forEach(r->{
            DeviceSquareVo vo=new DeviceSquareVo();
            vo.setRobotSN(r.get("robot_sn").toString());
            vo.setDeviceId(r.get("device_id").toString());
            if(period.equals(Period.Type.hour.toString())){
                vo.setReportDate(r.get("hour").toString());
                vo.setTotal(Float.valueOf(r.get("square").toString()));
            }else {
                vo.setReportDate(r.get("report_date").toString());
                vo.setTotal(Float.valueOf(r.get("total").toString()));
            }
            res.add(vo);
        });
        return res;
    }
    @ApiOperation("设备清扫面积")
    @PostMapping("/device_square/{deviceId}/{period}/{beginDate}/{endDate}")
    public List<DeviceSquareVo> getJobStatistics(@PathVariable String deviceId,@PathVariable String period,@PathVariable String beginDate,@PathVariable String endDate) {
        return this.getSquareByDevice(deviceId,period,beginDate,endDate);
    }

    @ApiOperation("下载工作成果-设备清扫面积")
    @PostMapping("/device_square/export/{deviceId}/{period}/{beginDate}/{endDate}")
    public void exportJobStatistics(HttpServletResponse response,@PathVariable String deviceId,@PathVariable String period,@PathVariable String beginDate,@PathVariable String endDate) {
        List<DeviceSquareVo> res= this.getSquareByDevice(deviceId,period,beginDate,endDate);
        ExcelUtil.exportExcel(res, "工作成果", DeviceSquareVo.class, response);
    }

    @ApiOperation("清扫统计")
    @PostMapping("/clean_statistics/{period}/{date}")
    public List<Map<String,Object>> getCleanStatistics(@PathVariable String period,@PathVariable String date) {
        List<Map<String,Object>> result= cleanStatisticsRepository.getStatistics(period,date);
        return result;
    }

    /**
     *围栏外警告
     * @param minute 分钟数，查询多少分钟内的告警设备数量
     * @return
     */
    @ApiOperation("围栏外警告")
    @PostMapping("/fence_alert/{minute}")
    public Integer getCleanStatistics(@PathVariable Integer minute) {
        Integer result= fenceAlertRepository.getStatistics(minute);
        return result;
    }

    @ApiOperation("获取所有设备清扫面积")
    @PostMapping("/clean_square")
    public List<Map<String,Object>> getCleanSquare(@RequestBody @Validated Request<QueryParamsVo> request) {
        List<Map<String,Object>> result= deviceStatisticsRepository.getCleanSquare(request.getData());
        return result;
    }

    @ApiOperation("导出所有设备清扫统计")
    @PostMapping("/export_clean_data")
    public void exportCleanDate(@RequestBody @Validated Request<QueryParamsVo> request, HttpServletResponse response) {
        List<Map<String,Object>> result= deviceStatisticsRepository.getCleanSquare(request.getData());
        List<CleanSquareVo> list=new ArrayList<>();
        result.forEach(r-> {
            CleanSquareVo vo= BeanUtil.toBean(r, CleanSquareVo.class);
            list.add(vo);
        });
        ExcelUtil.exportExcel(list, "清扫统计表", CleanSquareVo.class, response);
    }

    @ApiOperation("按月统计部门清扫、洗地数据")
    @PostMapping("/monthly_device_clean")
    public List<Map<String,Object>> getMonthlyDeptDeviceCleanData(@RequestBody @Validated Request<QueryParamsVo> request) {
        List<Map<String,Object>> result= deviceStatisticsRepository.getMonthlyDeptDeviceCleanData(request.getData());
        return result;
    }

    @ApiOperation("导出按月统计区域清扫、洗地数据")
    @PostMapping("/export_monthly_device_clean")
    public void exportMonthlyDeptDeviceCleanData(@RequestBody @Validated Request<QueryParamsVo> request, HttpServletResponse response) throws IOException {
        List<Map<String,Object>> result= deviceStatisticsRepository.getMonthlyDeptDeviceCleanData(request.getData());
        List<MonthlyCleanVo> list=new ArrayList<>();
        result.forEach(r-> {
            MonthlyCleanVo vo= BeanUtil.toBean(r, MonthlyCleanVo.class);
            list.add(vo);
        });
        ExcelUtil.exportExcel(list, "区域清扫、洗地数据按月统计表", MonthlyCleanVo.class, response);
        //调用HutoolExcelUtils导出方法
        //HuToolExcelUtils.exportListToExcel(list, "data");

    }
}
